package cn.tedu.submarine;

import java.awt.*;
import java.util.Random;
import javax.swing.ImageIcon;

public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected  int state = LIVE;//当前状态（默认为活着的）

    protected int width;//宽
    protected int height;//高
    protected int x;
    protected int y;
    protected int speed;//移动速度

    //潜艇
    public SeaObject(int width, int height) {
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT - height - 150) + 150;//150到（窗口高-潜艇高）
        speed = rand.nextInt(3) + 1;
    }

    //战舰、鱼雷
    public SeaObject(int width, int height, int x, int y, int speed) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void step();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return  state==LIVE;
    }
    public boolean isDead(){
        return state==DEAD;
}

    public void paintImage(Graphics g){
        if (isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }


    public SeaObject shootThunder(){
        int x = this.x+this.width;
        int y = this.y-5;
        if (this instanceof TorpedoSubmarine){//若当前对象为鱼雷潜艇
            return new Torpedo(x,y);
        }else if (this instanceof MineSubmarine){
            return new Mine(x,y);
        }else {
            return null;
        }
    }

    public boolean isOutOfBounds(){//检测潜艇越界
        return this.x>=World.WIDTH;
    }

    public boolean isHit(SeaObject other){
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;
        int y = other.y;

        return x>=x1 && x<=x2
                &&
                y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;
    }
}
